<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Task;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;

class TaskController extends Controller
{
    /**
     * Display a listing of the resource with pagination and status filtering.
     */
    public function index(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'per_page' => 'nullable|integer|min:1|max:100',
            // 根据表结构，增加状态筛选的验证
            'status' => 'nullable|string|in:pending,in_progress,completed,on_hold,cancelled,overdue',
            'title' => 'nullable|string'
        ]);

        $perPage = $validated['per_page'] ?? 10;
        $query = Task::query();

        // 根据请求参数筛选任务状态
        if ($request->has('status')) {
            $query->where('status', $validated['status']);
        }

        // 添加对 title 的模糊搜索
        if ($request->has('title')) {
            $query->where('title', 'like', '%'.$validated['title'].'%');
        }
        
        $tasks = $query->paginate($perPage);

        return response()->json($tasks, 200);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            // 根据表结构，增加状态验证
            'status' => 'nullable|string|in:pending,in_progress,completed,on_hold,cancelled,overdue',
        ]);

        $task = Task::create($validated);

        return response()->json($task, 201);
    }

    /**
     * Display the specified resource.
     */
    public function show($id): JsonResponse
    {
        $task = Task::findOrFail($id);
        return response()->json($task, 200);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request,int $id): JsonResponse
    {
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
        ]);

        $task = Task::findOrFail($id);
        $task->update($validated);

        return response()->json($task, 200);
    }

    /**
     * Update the status of the specified resource in storage.
     * 专门用于更新任务状态的独立方法。
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function updateStatus(Request $request, int $id): JsonResponse
    {
        $validated = $request->validate([
            'status' => 'required|string|in:pending,in_progress,completed,on_hold,cancelled,overdue',
        ]);

        $task = Task::findOrFail($id);
        $task->update($validated);

        return response()->json($task, 200);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id): Response
    {
        $task = Task::findOrFail($id);
        $task->delete();
        return response()->noContent();
    }

    /**
     * Remove multiple specified resources from storage.
     * 从存储中删除多个指定的资源.
     * @param Request $request
     * @return Response
     */
    public function batchDestroy(Request $request): Response {
        $validated = $request->validate([
            'ids' => 'required|array',
            'ids.*' => 'required|integer|exists:tasks,id'
        ]);

        $deletedCount = Task::whereIn('id', $validated['ids'])->delete();

        return response()->noContent();
    }
}